package com.keliangliu.demo.decode;

import org.joda.time.DateTime;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Base64;

public class BusDecode {


    /**
     * 解析车站序号
     *
     * @param encodeSeq
     * @returns String
     */
    public static String decodeBusSeq(String encodeSeq) {
        DateTime now = DateTime.now();
        int dateNum = Integer.valueOf(now.toString("yyyyMMdd"));

        int weekNum = now.getDayOfWeek() + 1;
        // 先拆掉变换次数
        int transfer = dateNum % weekNum + 1;


        char[] seqChars = encodeSeq.toCharArray();

        for (int i = 0; i < transfer; i++) {
            for (int j = 0; j < seqChars.length; j++) {
                seqChars[j] = transferBusSeq(seqChars[j]);
            }
        }


        String seq = new String(seqChars);
        // 脱去最后一位随机数
        int random = Integer.valueOf(seq.substring(seq.length() - 1));
        // 获取当前系统时间的日期数字
        String result = new BigDecimal(seq.substring(0, seq.length() - 1)).subtract(new BigDecimal(dateNum)).toString();

        // 准备拆分解析数，脱出序列号Int
        return Integer.valueOf(result.substring(random,random + 3)).toString();
    }

    /**
     * 转换值
     *
     * @param c
     * @return
     */
    private static char transferBusSeq(char c) {
        switch (c) {
            case '0':
                return '4';
            case '1':
                return '9';
            case '2':
                return '6';
            case '3':
                return '8';
            case '4':
                return '0';
            case '5':
                return '1';
            case '6':
                return '3';
            case '7':
                return '5';
            case '8':
                return '2';
            case '9':
                return '7';
            default:
                return c;
        }


    }


    /**
     * 解析车辆相对位置
     * @param position
     * @returns {string}
     */
    public static String decodeBusPosition (String position) {

        // 先拆除随机数
        int random = Integer.valueOf(position.substring(position.length() - 1));
        // 混淆数据序列是
        position = position.substring(0, position.length() - 1);
        DateTime now = DateTime.now();
        int dateNum = Integer.valueOf(now.toString("yyyyMMdd"));
        int seconds = (int) (DateTime.parse(now.toString("yyyy-MM-dd")).toDate().getTime() / 1000);
        // 变换次数由秒级别时间戳与周次的余数决定
        int transferNum = seconds %  now.getDayOfWeek()  + 1;
        // 开始对序列进行逆转变换
        char[] chars = position.toCharArray();
        for (int i = 0; i < transferNum; i++) {
            for (int j = 0; j < chars.length; j++) {
                chars[j] = transferPosition(chars[j]);
            }
        }
        String temp = new String(chars);
        transferNum = Integer.valueOf(temp) / random - dateNum;
        return "0." + transferNum;
    }

    /**
     * 转换车辆位置信息
     * @param c
     * @return
     */
    private static char transferPosition(char c) {
        switch (c) {
            case '0':
                return '9';
            case '1':
                return '6';
            case '2':
                return '8';
            case '3':
                return '4';
            case '4':
                return '2';
            case '5':
                return '0';
            case '6':
                return '7';
            case '7':
                return '5';
            case '8':
                return '1';
            case '9':
                return '3';
            default:
                return c;
        }
    }


    public static void main(String[] args) {
        System.out.println(decodeBusPosition("8143506128"));
    }
}




